package com.javandad.app.server.servlet.http;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javandad.app.server.db.DatabaseManager;
import com.javandad.app.server.db.PMFSingleton;
import com.javandad.app.server.entity.Fodo;

@SuppressWarnings("serial")
public class ImageDownloader extends HttpServlet {


	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		shared(req, resp);
		
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		// Get the image representation
		shared(req, res);
		
	}

	private void shared(HttpServletRequest req, HttpServletResponse res) {
	
		
		try{
			String id_string = req.getParameter("id");
		    long id = Long.valueOf(id_string);
		    DatabaseManager dbm = DatabaseManager.getDefaultDatabaseManager(); 
		    Fodo fodo = dbm.getFodo(null, id, PMFSingleton.getPM());
		    
		    res.setContentType("image/jpg");
			OutputStream o = res.getOutputStream();						
			o.write(fodo.getContent().getBytes());			
			o.flush();
			o.close();
		} catch (Exception e) {

		}
	}
}
